package poker.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.*;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;

import poker.Card;
import poker.GameRound;
import poker.Human;
import poker.Player;

public class GUI_Table extends JPanel {
	private static final long serialVersionUID = 1L;
	final public static String carddir = "src/cards/";
	final public static String cardext = ".png";

	GameRound gameRound;
	JPanel masterPanel;
	JPanel startPanel;	
	JPanel status;

	int[] xCoord;
	int[] yCoord;

	public GUI_Table(GameRound gameRound, boolean visible) {
		super();
		
		JFrame f = new JFrame("Table");
		masterPanel = new JPanel();
		startPanel = new JPanel();
		status = new JPanel();
		this.gameRound = gameRound;
		
		startPanel.setBackground(new Color(0x00, 0x64, 0x00));
		
		xCoord = new int[10];
		yCoord = new int[10];
		fillCoordinates();

		WindowListener wndCloser = new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		};
		f.addWindowListener(wndCloser);
		f.setContentPane(masterPanel);

		BorderLayout borderLayout = new BorderLayout();
		BorderLayout statusBorderLayout = new BorderLayout();

		masterPanel.setLayout(borderLayout);
		status.setLayout(statusBorderLayout);

		startPanel.setPreferredSize(new Dimension(900, 700));
		masterPanel.add(startPanel,  BorderLayout.WEST);		

		f.setSize(900, 700);
		f.setVisible(visible);
		f.setResizable(false);
	}

	public boolean isFirstPosAI(GameRound gameRound)
	{
		ArrayList<Player> players = (ArrayList<Player>) gameRound.getPlayers();
		if(players.get(0).getisAI()){ return true;}
		
		return false;		
	}
		
	private void drawFlop(Graphics g, Card[] card)

	{
		File source1 = new File(carddir + card[0].toString() + cardext);
		File source2 = new File(carddir + card[1].toString() + cardext);
		File source3 = new File(carddir + card[2].toString() + cardext);

		Image image1 = new ImageIcon(source1.toString()).getImage();
		Image image2 = new ImageIcon(source2.toString()).getImage();
		Image image3 = new ImageIcon(source3.toString()).getImage();

		g.drawImage(image1, 225 , 260, 80, 80, this);
		g.drawImage(image2, 225 + 90, 260, 80, 80, this);
		g.drawImage(image3, 225 + 90 + 90, 260, 80, 80, this);

	}

	private void drawTurn(Graphics g, Card card)
	{
		File source1 = new File(carddir + card.toString() + cardext);
		Image image1 = new ImageIcon(source1.toString()).getImage();	
		g.drawImage(image1, 225 + 90 + 90 + 90, 260, 80, 80, this);
	}

	private void drawRiver(Graphics g, Card card)
	{
		File source1 = new File(carddir + card.toString() + cardext);
		Image image1 = new ImageIcon(source1.toString()).getImage();	
		g.drawImage(image1, 225 + 90 + 90 + 90 + 90, 260, 80, 80, this);
	}

	private void drawPot(Graphics g, int pot)
	{
		g.clearRect(405, 420, 50, 20);
		g.drawString(Integer.toString(pot), 405, 440);		
	}

	private void drawCard(Graphics g, Player player, int x, int y, GameRound gameRound)
	{
		File source1 = null;
		File source2 = null;
		File button = null;
		Image imageButton = null;
		Card[] card = player.getPocketCards();

		if (card[0] == null) {
			//Show card back.
			source1 = new File(carddir + "backCard" + cardext);
			source2 = new File(carddir + "backCard" + cardext);
		} 
		else {
			// Real card.
			source1 = new File(carddir + card[0].toString() + cardext);
			source2 = new File(carddir + card[1].toString() + cardext);
		}

		if(player == gameRound.getDealer()){
			button = new File(carddir + "dealerButton" + cardext);
			imageButton = new ImageIcon(button.toString()).getImage();
		}
		if(player == gameRound.getPlayers().get(gameRound.getSmallBlindPosition())){
			button = new File(carddir + "smallBlind" + cardext);
			imageButton = new ImageIcon(button.toString()).getImage();
		}
		if(player == gameRound.getPlayers().get(gameRound.getBigBlindPosition())){
			button = new File(carddir + "bigBlind" + cardext);
			imageButton = new ImageIcon(button.toString()).getImage();
		}

		Image image1;
		Image image2;
		// Draw the card.
		if(!player.getFolded())
		{
			image1 = new ImageIcon(source1.toString()).getImage();
			image2 = new ImageIcon(source2.toString()).getImage();
		}
		else
		{
			File backCard = new File(carddir + "backCard" + cardext);
			image1 = new ImageIcon(backCard.toString()).getImage();
			image2 = new ImageIcon(backCard.toString()).getImage();
		}

		g.drawImage(image1, x, y, 80, 80, this);
		g.drawImage(image2, x + 90, y, 80, 80, this);
		g.drawString(player.getName(), x + 50, y + 100);
	}


	public void updateGUI(GameRound gameRound)
	{
		this.gameRound = gameRound;
		paintComponent(startPanel.getGraphics());
		
		repaint();
		updateUI();
		revalidate();		
	}

	private void fillCoordinates()
	{

		xCoord[0] = 0;
		xCoord[1] = 225;
		xCoord[2] = 450;
		xCoord[3] = 720;
		xCoord[4] = 720;
		xCoord[5] = 720; 
		xCoord[6] = 450;
		xCoord[7] = 225;
		xCoord[8] = 0;
		xCoord[9] = 0; 

		yCoord[0] = 0;
		yCoord[1] = 0;
		yCoord[2] = 0;
		yCoord[3] = 0;
		yCoord[4] = 260;
		yCoord[5] = 520; 
		yCoord[6] = 520;
		yCoord[7] = 520;
		yCoord[8] = 520;
		yCoord[9] = 260; 
	}

	public void paintComponent(Graphics g) 
	{		

		for (int i = 0; i < gameRound.getPlayers().size(); i++) 
		{
			drawCard(g, gameRound.getPlayers().get(i), xCoord[i], yCoord[i], gameRound);
			repaint();
		}

		//Update players cash
		for (int i = 0; i < gameRound.getPlayers().size(); i++) {

			g.clearRect(xCoord[i] + 50, yCoord[i] + 110, 50, 15);
			g.drawString(Integer.toString(gameRound.getPlayers().get(i).getCash()), xCoord[i] + 50, yCoord[i] + 120);

			g.clearRect(405, 420, 30, 20);
			drawPot(g, gameRound.getPot());
			repaint();
		}

		if(gameRound.getFlopDealt()){ drawFlop(g, gameRound.getFlop());	}
		if(gameRound.getTurnDealt()){ drawTurn(g, gameRound.getTurnCard());}
		if(gameRound.getRiverDealt()){ drawRiver(g, gameRound.getRiverCard());}
	}

	public void queryNewRound() 
	{
		int choice = JOptionPane.showConfirmDialog(this, "Do you want to play another round?", "New round?", JOptionPane.YES_NO_OPTION);
		if(choice == 0)
		{
			gameRound.start();
		}
	}
}

